package com.vx.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.activitiFlow.service.IRepositoryBiz;
import com.vx.form.WorkFlowBean;
import com.vx.service.IWorkflowService;
import com.vx.vo.UserVO;

/**
 * 创建管理流程的控制类
 * @author jacob
 *
 */
@Controller
@RequestMapping("/repository")
public class RepositoryController {
	
	@Autowired
	private IRepositoryBiz repositoryBiz;
	
	
	/**
	 * 查看所有部署的流程 
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	public String list(Model model,HttpServletRequest request){
		List<Deployment> depList=repositoryBiz.queryDeploymentList();
		List<ProcessDefinition> proList=repositoryBiz.queryProcessDefinitionList();
		model.addAttribute("depList", depList);
		model.addAttribute("proList", proList);
		return "workflow/workflow"; 
	}
	
	/**
	 * 根据上传文件 新增流程
	 * @param filename
	 * @param file
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/newdeploy",method=RequestMethod.POST)
	public String newdeploy(String filename,@RequestParam MultipartFile file,HttpServletRequest request){
		System.out.println("newdeploy...");
		try {
			repositoryBiz.saveNewDeploy(filename,file.getInputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return "redirect:/repository/list"; 
	}
	
	/**
	 * 查看流程图
	 * @param deploymentId
	 * @param imageName
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="findDeployImg")
	public void findDeployImg(String deploymentId,String imageName,Model model,HttpServletRequest request,HttpServletResponse response){
		InputStream in=repositoryBiz.findDeployImg(deploymentId,imageName);
		try {
			OutputStream out=response.getOutputStream();
			for(int b=-1;(b=in.read())!=-1;){
				out.write(b);
			}
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 删除流程
	 * @param deploymentId
	 * @param model
	 * @param reques
	 * @return
	 */
	@RequestMapping("deleteDeploy")
	public String deleteDeploy(String deploymentId,Model model,HttpServletRequest reques){
		repositoryBiz.deleteDeploy(deploymentId);
		return "workflow/workflow"; 
	}

	@RequestMapping("getFlowAll")
	@ResponseBody
	public List<ProcessDefinition> getFlowAll(Model model,HttpServletRequest reques){
		List<ProcessDefinition> proList=repositoryBiz.queryProcessDefinitionList();
		return proList;
	}
	
}
